package com.changanw.tienchin.web.controller.tienchin.business;

import com.changanw.tienchin.activity.service.IActivityService;
import com.changanw.tienchin.business.domain.Business;
import com.changanw.tienchin.business.domain.vo.BusinessFollow;
import com.changanw.tienchin.business.domain.vo.BusinessSummary;
import com.changanw.tienchin.business.domain.vo.BusinessSummaryEnhance;
import com.changanw.tienchin.business.domain.vo.BusinessVO;
import com.changanw.tienchin.business.service.IBusinessService;
import com.changanw.tienchin.channel.service.IChannelService;
import com.changanw.tienchin.common.annotation.Log;
import com.changanw.tienchin.common.core.controller.BaseController;
import com.changanw.tienchin.common.core.domain.AjaxResult;
import com.changanw.tienchin.common.core.page.TableDataInfo;
import com.changanw.tienchin.common.enums.BusinessType;
import com.changanw.tienchin.course.service.ICourseService;
import com.changanw.tienchin.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author changanw
 * @since 2023-12-06
 */
@RestController
@RequestMapping("/tienchin/business")
public class BusinessController extends BaseController {
  @Autowired
  IBusinessService businessService;
  @Autowired
  IChannelService channelService;
  @Autowired
  IActivityService activityService;
  @Autowired
  ISysUserService userService;
  @Autowired
  ICourseService courseService;
  @PreAuthorize("hasPermission('tienchin:business:list')")
  @GetMapping("/list")
  public TableDataInfo list(BusinessVO businessVO) {
    startPage();
    List<BusinessSummary> list = businessService.selectBusinessList(businessVO);
    return getDataTable(list);
  }

  /**
   * 新增商机
   */
  @PreAuthorize("hasPermission('tienchin:business:create')")
  @Log(title = "商机管理", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@Validated @RequestBody Business business) {
    return businessService.addBusiness(business);
  }
  /**
   * 返回所有渠道
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:business:create')")
  @GetMapping("/channels")
  public AjaxResult getAllChannel(){
    return AjaxResult.success(channelService.list());
  }

  /**
   * 根据渠道id  获取活动
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:business:create')")
  @GetMapping("/activity/{channelId}")
  public AjaxResult getActivityByChannelId(@PathVariable Integer channelId){
    return activityService.selectActivityByChannelId(channelId);
  }

  /**
   * 根据课程类型获取课程
   * @param courseType
   * @return
   */
  @PreAuthorize("hasAnyPermissions('tienchin:business:view','tienchin:business:follow')")
  @GetMapping("/course/{courseType}")
  public AjaxResult getCourseByCourseType(@PathVariable Integer courseType){
    return courseService.getCourseByCourseType(courseType);
  }
  //获取所有课程
  @PreAuthorize("hasAnyPermissions('tienchin:business:view','tienchin:business:follow')")
  @GetMapping("/all_course")
  public AjaxResult getAllCourse(){
    return AjaxResult.success(courseService.list());
  }

  /**
   * 根据商机id  获取具体信息
   * @param businessId
   * @return
   */
  @PreAuthorize("hasAnyPermissions('tienchin:business:view','tienchin:business:follow')")
  @GetMapping("/{businessId}")
  public AjaxResult getBusinessById(@PathVariable Integer businessId){
    return businessService.getBusinessById(businessId);
  }

  @PreAuthorize("hasPermission('tienchin:business:follow')")
  @PostMapping("/follow")
  public AjaxResult businessFollow(@RequestBody BusinessFollow businessFollow){
    return businessService.follow(businessFollow);
  }

  @GetMapping("/summary/{businessId}")
  @PreAuthorize("hasPermission('tienchin:business:edit')")
  public AjaxResult getBusinessSummaryByBusinessId(@PathVariable Integer businessId) {
    return businessService.getBusinessSummaryByBusinessId(businessId);
  }

  /**
   * 更新商机
   * @param business
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:business:edit')")
  @Log(title = "商机管理", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult updateClue(@RequestBody BusinessSummaryEnhance businessSummaryEnhance){
    return businessService.updateClue(businessSummaryEnhance);
  }
  /**
   * 删除商机
   */
  @PreAuthorize("hasPermission('tienchin:business:remove')")
  @Log(title = "商机管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/{businessIds}")
  public AjaxResult remove(@PathVariable Integer[] businessIds) {
    return businessService.deleteBusinessByIds(businessIds);
  }

}
